package com.i2works.smartluolong.manage.controller.flow;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.i2works.smartluolong.manage.utils.BaseModule;
import com.i2works.smartluolong.utils.base.MsgEnum;
import com.i2works.smartluolong.utils.entity.R;
import com.i2works.smartluolong.utils.entity.Result;
import com.i2works.smartluolong.web.entity.flow.FlowDO;
import com.i2works.smartluolong.web.entity.flow.FlowQuery;
import com.i2works.smartluolong.web.service.flow.IFlowService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 基础流程控制器
 *
 * @author Liu Yutao <koal@vip.qq.com>
 * @date 2018/3/29 9:59
 */

@Controller
@RequestMapping("/flow")
public class FlowController extends BaseModule {

    private final IFlowService flowService;

    public FlowController(IFlowService flowService) {
        this.flowService = flowService;
    }

    /**
     * 基础流程页面
     */
    @GetMapping(value = "/view/index")
    public String index() {
        logger.info("基础流程页面");
        return "/flow/flow";
    }


    /**
     * 基础流程列表
     */
    @GetMapping(value = "/view/api")
    @ResponseBody
    public Result queryList(@ModelAttribute FlowQuery query) {
        Result result = new Result();
        try {
            int total = flowService.queryCount(query);
            List<FlowDO> flowList = Lists.newArrayList();
            if (total > 0) {
                flowList = flowService.queryList(query);
            }
            return R.success(flowList, total);
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.SYSTEM_ERROR.getMsg());
        }
    }


    /**
     * 新增编辑基础流程跳转
     */
    @GetMapping(value = "/save/form")
    public String savePage() {
        logger.info("基础流程新增编辑");
        return "/flow/flow_form";
    }


    /**
     * 添加基础流程
     */
    @PostMapping("/save/action")
    @ResponseBody
    public Result save(@ModelAttribute FlowDO flowDO) {
        try {
            flowDO.setUpdateName(getUserName());
            flowDO.setCreateName(getUserName());
            flowService.saveOrUpdate(flowDO);
            return R.success();
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.SAVE_ERROR.getMsg());
        }
    }


    /**
     * 查询流程
     */
    @GetMapping(value = "/view/{id}")
    @ResponseBody
    public Result query(@PathVariable Integer id) {
        try {
            FlowQuery query = new FlowQuery();
            query.setId(id);
            FlowDO flow = flowService.query(query);
            return R.success(flow);
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.FIND_ERROR.getMsg());
        }
    }

    /**
     * 某个基础流程（名称、标示、代码）是否存在验证
     */
    @PostMapping("/view/check")
    @ResponseBody
    public boolean queryCheck(@ModelAttribute FlowQuery query) {
        try {
            int count = flowService.queryCheck(query);
            return count <= 0;
        } catch (Exception e) {
            logger.error("", e);
        }
        return false;
    }

    /**
     * 更新状态
     */
    @PostMapping("/status/action")
    @ResponseBody
    public Result updateStatus(@ModelAttribute FlowDO flowDO) {
        Result result = new Result();
        try {
            flowDO.setUpdateName(getUserName());
            flowDO.setCreateName(getUserName());
            flowService.updateStatus(flowDO);
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.STATUS_ERROR.getMsg());
        }
        return R.success();
    }

    /**
     * 批量更新状态
     */
    @PostMapping("/status/batchAction")
    @ResponseBody
    public Result updateBatchStatus(@RequestParam("idsList[]") List<Integer> idsList, Integer status) {
        Result result = new Result();
        try {
            if (idsList != null && idsList.size() > 0) {
                Map<String, Object> map = Maps.newHashMap();
                map.put("idsList", idsList);
                map.put("updateName", getUserName());
                map.put("status", status);
                flowService.updateBatchStatus(map);
            }
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.STATUS_ERROR.getMsg());
        }
        return result;
    }


    /**
     * 删除
     */
    @PostMapping("/delete/action")
    @ResponseBody
    public Result delete(@RequestParam Integer id) {
        try {
            flowService.delete(id);
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.DELETE_ERROR.getMsg());
        }
        return R.success();
    }

    /**
     * 批量删除
     */
    @PostMapping("/delete/batchAction")
    @ResponseBody
    public Result deleteBatch(@RequestParam("idsList[]") List<Integer> idsList) {
        try {
            if (idsList != null && idsList.size() > 0) {
                flowService.deleteBatch(idsList);
            } else {
                return R.error(MsgEnum.ATTRS_EMPTY.getMsg());
            }
        } catch (Exception e) {
            logger.error("", e);
            return R.error(MsgEnum.DELETE_ERROR.getMsg());
        }
        return R.success();
    }


}
